﻿//
// CommandGroup.cs
//
// Authors:
//  Kenny York <kennyyork@gmail.com>
//
// Copyright (C) 2009 Kenny York
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace clidotnet
{
    /// <summary>
    /// A CommandGroup is a subgroup in the command list
    /// </summary>
    public class CommandGroup : System.Collections.Generic.IEnumerable<ICommand>, ICommand, ICommandInitializer
    {
        public string Name { get { return commandName; } }                         
        public string Help { get; set; }
        private string commandName;
        private List<ICommand> baseGroup;

        /// <summary>
        /// Create a command group with no help
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        public CommandGroup(string name) : this(name, "") { }

        /// <summary>
        /// Create a command group
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        public CommandGroup(string name, string help)
        {
            baseGroup = new List<ICommand>();
            commandName = name;
            Help = help;
        }

        /// <summary>
        /// Parse the command group for the provided commands
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public void Execute(List<string> args)
        {
            throw new InvalidOperationException("Cannot execute a CommandGroup");
        }

        public bool Equals(ICommand command)
        {
            if (command.Name == Name)
            {
                return true;
            }
           
            return false;
        }

        public ICommand Implements(string command, List<string> args)
        {
            ICommand exec = null;

            if (command == Name)
            {
                if (args.Count > 0)
                {
                    string newCommand = args[0];
                    args.RemoveAt(0);

                    foreach (ICommand c in baseGroup)
                    {
                        exec = c.Implements(newCommand, args);
                        if (exec != null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    //print list of commands in this group
                }
            }

            return exec;
        }

        public bool Implements(string command)
        {
            return (Name == command);
        }

        /// <summary>
        /// Check if the command exists within this CommandGroup.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public bool Contains(ICommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException();
            }

            foreach (ICommand c in baseGroup)
            {
                if (c.Equals(command))
                {
                    return true;
                }
            }

            return false;
        }

        private void ValidateCommand(ICommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException();
            }

            if (Contains(command))
            {
                throw new DuplicateCommandException(command);
            }
        }

        public string ToString(bool recursive)
        {
            StringBuilder sb = new StringBuilder();
            foreach (ICommand c in baseGroup)
            {
                if (recursive && (c is CommandGroup))
                {
                    sb.AppendLine(((CommandGroup)c).ToString(true));
                }
                else
                {
                    sb.AppendLine(c.ToString());
                }
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }

        public override string ToString()
        {
            return Name;
        }

        public void Add(ICommand command)
        {
            ValidateCommand(command);
            baseGroup.Add(command);
        }

        #region ICommandInitializer Members

        /// <summary>
        /// Adds a CommandGroup to the list of ICommand objects
        /// </summary>
        /// <param name="group">The CommandGroup to add</param>
        /// <exception cref="ArgumentNullException">Thrown when group is null.</exception>
        /// <exception cref="ArgumentException">Thrown when the named given by group is already in use.</exception>
        public void Add(CommandGroup group)
        {
            Add((ICommand)group);
        }

        /// <summary>
        /// Add a Command with no parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add(string name, CommandLineAction action)
        {
            ICommand command = new Command(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with no parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add(string name, string help, CommandLineAction action)
        {
            ICommand command = new Command(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command to the CommandList
        /// </summary>
        /// <param name="command"></param>
        public void Add(Command command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with one parameter
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T>(string name, CommandLineAction<T> action)
        {
            ICommand command = new Command<T>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with one parameter
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T>(string name, string help, CommandLineAction<T> action)
        {
            ICommand command = new Command<T>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with one parameter
        /// </summary>
        /// <param name="command"></param>
        public void Add<T>(Command<T> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with two parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1>(string name, CommandLineAction<T0, T1> action)
        {
            ICommand command = new Command<T0, T1>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with two parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1>(string name, string help, CommandLineAction<T0, T1> action)
        {
            ICommand command = new Command<T0, T1>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with two parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1>(Command<T0, T1> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with three parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2>(string name, CommandLineAction<T0, T1, T2> action)
        {
            ICommand command = new Command<T0, T1, T2>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with three parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2>(string name, string help, CommandLineAction<T0, T1, T2> action)
        {
            ICommand command = new Command<T0, T1, T2>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with three parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2>(Command<T0, T1, T2> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with four parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3>(string name, CommandLineAction<T0, T1, T2, T3> action)
        {
            ICommand command = new Command<T0, T1, T2, T3>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with four parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3>(string name, string help, CommandLineAction<T0, T1, T2, T3> action)
        {
            ICommand command = new Command<T0, T1, T2, T3>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with four parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3>(Command<T0, T1, T2, T3> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with five parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4>(string name, CommandLineAction<T0, T1, T2, T3, T4> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with five parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4>(string name, string help, CommandLineAction<T0, T1, T2, T3, T4> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with five parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3, T4>(Command<T0, T1, T2, T3, T4> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with six parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5>(string name, CommandLineAction<T0, T1, T2, T3, T4, T5> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with six parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5>(string name, string help, CommandLineAction<T0, T1, T2, T3, T4, T5> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with six parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3, T4, T5>(Command<T0, T1, T2, T3, T4, T5> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with seven parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6>(string name, CommandLineAction<T0, T1, T2, T3, T4, T5, T6> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with seven parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6>(string name, string help, CommandLineAction<T0, T1, T2, T3, T4, T5, T6> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with seven parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3, T4, T5, T6>(Command<T0, T1, T2, T3, T4, T5, T6> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with eight parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7>(string name, CommandLineAction<T0, T1, T2, T3, T4, T5, T6, T7> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6, T7>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with eight parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7>(string name, string help, CommandLineAction<T0, T1, T2, T3, T4, T5, T6, T7> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6, T7>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with eight parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7>(Command<T0, T1, T2, T3, T4, T5, T6, T7> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with nine parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7, T8>(string name, CommandLineAction<T0, T1, T2, T3, T4, T5, T6, T7, T8> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6, T7, T8>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with nine parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7, T8>(string name, string help, CommandLineAction<T0, T1, T2, T3, T4, T5, T6, T7, T8> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6, T7, T8>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with nine parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7, T8>(Command<T0, T1, T2, T3, T4, T5, T6, T7, T8> command)
        {
            Add((ICommand)command);
        }

        /// <summary>
        /// Add a Command with ten parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(string name, CommandLineAction<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(name, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with ten parameters
        /// </summary>
        /// <param name="name">Pipe (|) delimited list of names used to call the command</param>
        /// <param name="help">Help text (optional)</param>
        /// <param name="action">The action to perform when <pararef name="name"/> is called.</param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(string name, string help, CommandLineAction<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> action)
        {
            ICommand command = new Command<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(name, help, action);
            Add(command);
        }

        /// <summary>
        /// Add a Command with ten parameters
        /// </summary>
        /// <param name="command"></param>
        public void Add<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(Command<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> command)
        {
            Add((ICommand)command);
        }

        #endregion        
    
        #region IEnumerable<ICommand> Members

        public IEnumerator<ICommand> GetEnumerator()
        {
            return new ReadOnlyCollection<ICommand>(baseGroup).GetEnumerator();            
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
